package com.zwz.service.impl;

import com.zwz.dto.BusinessAbnormalityDTO;
import com.zwz.entity.BusinessAbnormality;
import com.zwz.entity.EnterpriseInfo;
import com.zwz.entity.RiskInfoMain;
import com.zwz.mapper.BusinessAbnormalityMapper;
import com.zwz.mapper.RiskInfoMainMapper;
import com.zwz.service.BusinessAbnormalityService;
import com.zwz.voObject.MangeInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class BusinessAbnormalityServiceImpl implements BusinessAbnormalityService {

    private final BusinessAbnormalityMapper mapper;

    @Autowired
    private  RiskInfoMainMapper  riskInfoMainMapper;

    @Autowired
    public BusinessAbnormalityServiceImpl(BusinessAbnormalityMapper mapper) {
        this.mapper = mapper;
    }

//    @Override
//    public void saveAbnormality(BusinessAbnormality record) {
//      //  validateAbnormality(record);
//
//        // 1.先插入主表
//
//        int i=0;
//        record.setId(i++);
//        RiskInfoMain main;
//        riskInfoMainMapper.insert(main);
//        //2.再插入子表
//        //2.1 先自动维护
//
//        mapper.insert(record);
//    }
//
//    @Override
//    public void updateAbnormality(BusinessAbnormality record) {
//
//    }

    @Override
    public void saveAbnormality(BusinessAbnormality record) {

    }

    @Override
    public void updateAbnormality(BusinessAbnormality record) {

    }

    @Override
    public void removeAbnormality(Integer id) {

    }

    @Override
    public BusinessAbnormality getAbnormalityById(Integer id) {
        return null;
    }

    @Override
    public List<BusinessAbnormality> getCurrentAbnormalities() {
        return null;
    }

//    private void validateAbnormality(BusinessAbnormality record) {
//        if (record.getCurrentAbnormality() && record.getInclusionReason() == null) {
//            throw new IllegalArgumentException("当前异常必须填写原因");
//        }
//    }

//    @Override
//    public List<BusinessAbnormality> getCurrentAbnormalities() {
//        return mapper.selectCurrentAbnormalities();
//    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public BusinessAbnormalityDTO createWithMain( String enterpriseId,  BusinessAbnormality data) {

        // 1. 创建主表记录
        // 主表操作
        EnterpriseInfo enterpriseInfo=new EnterpriseInfo();
        enterpriseInfo.setId(Long.valueOf(enterpriseId));


        // 2. 绑定子表ID
//        BusinessAbnormality data = new BusinessAbnormality();
//         int  k=0;
//        data.setId(k);
//        data.setAbnormalityCount(1);
//        data.setHistoricalReasons("你好");
        // 3. 验证业务规则
//        validateAbnormalityData(data);

        data.setEnterpriseId(Math.toIntExact(enterpriseInfo.getId()));
        // 4. 插入子表数据
        mapper.insert(data);

        // 5. 返回组合结果
        return new BusinessAbnormalityDTO(enterpriseInfo, data);
    }

    @Override
    public MangeInfo getCurrentAbnormalitiesMangeInfo(String id) {
        return mapper.getMangeInfo(id);
    }

//    private void validateAbnormalityData(BusinessAbnormality data) {
//        if (data.getCurrentAbnormality() != null && data.getCurrentAbnormality()) {
//            if (data.getLatestInclusionDate() == null) {
//                throw new IllegalArgumentException("当前异常状态必须包含列入日期");
//            }
//            if (StringUtils.isBlank(data.getInclusionReason())) {
//                throw new IllegalArgumentException("当前异常状态必须填写原因");
//            }
//        }
//    }



}